package com.shioirikukuri.common.until;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.shioirikukuri.common.condig.QiniuConfig;

public class PictureUtil {

	/**
	 * 上传图片到七牛云
	 *
	 *
	 * @param picturePath
	 * @return 上传成功返回图片的URL，上传失败返回null
	 */
	public static String uploadPicture(String picturePath) {
		UploadManager uploadManager = QiniuConfig.uploadManager();
		String token = QiniuConfig.auth().uploadToken(QiniuConfig.getBucketName());

		try {
			Response response = uploadManager.put(picturePath, null, token);
			if (response.isOK()) {
				// 获取返回的 key，拼接域名
				String key = response.jsonToMap().get("key").toString();
				return "http://" + QiniuConfig.getDomain() + "/" + key;
			} else {
				System.err.println("上传失败：" + response.bodyString());
			}
		} catch (QiniuException ex) {
			System.err.println("上传异常：" + ex.response.toString());
		}
		return null;
	}

}
